﻿using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;
using System.Collections.Generic;

namespace DroidClaim.Web.CustomHelpers
{
    public static class HtmlExtensions
    {
        public static MvcHtmlString RadioButtonForSelectList<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression, SelectList RadioOptions)
        {
            var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            StringBuilder sb = new StringBuilder();
            if (RadioOptions != null)
            {
                // put a similar foreach here
                foreach (var myOption in RadioOptions)
                {
                    // Generate an id to be given to the radio button field 
                    var id = string.Format("{0}_{1}", metaData.PropertyName, myOption.Value);
                    sb.Append(htmlHelper.RadioButtonFor(expression, myOption.Value, new { id = id }) + myOption.Text);
                }
            }
            return new MvcHtmlString(sb.ToString());
        }

        public static MvcHtmlString LookupFor<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression, String className, String itemCode, String itemName)
        {
            StringBuilder sb = new StringBuilder();
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
             

            string ButtonName = className + "Btn";
            string SearchName = className + "Code";
            string FillDataName = className + "Name";
            sb.Append(String.Format("<input type=\"text\" id=\"{0}\" class=\"txtFillCode\" maxlength=\"10\" {1}/>", SearchName, itemCode != null && itemCode.Equals("") ? "" : "value=\"" + itemCode + "\""));
            sb.Append(String.Format("<input type=\"button\" id=\"{0}\" value=\"...\" class=\"btnLookup\" />", ButtonName));
            sb.Append(String.Format("<input type=\"text\" id=\"{0}\" class=\"txtFillName\" readonly=\"readonly\" {1}/>", FillDataName, itemName != null && itemName.Equals("") ? "" : "value=\"" + itemName + "\""));
            sb.Append(String.Format("<input type=\"hidden\" id=\"{0}GetLookup\" value=\"{1}\" />", className, urlHelper.Action(className+"GetLookup", "Popup")));
            sb.Append(String.Format("<input type=\"hidden\" id=\"{0}PopLookup\" value=\"{1}\" />", className, urlHelper.Action(className+"PopLookup", "Popup")));

            sb.Append(htmlHelper.HiddenFor(expression, new {id = className, @class = "TextLookup"}));

            return new MvcHtmlString(sb.ToString());
        }

        public static LookupHtmlBuilder<TModel> Lookup<TModel>(
            this HtmlHelper<TModel> htmlHelper, String className)
        {
            return new LookupHtmlBuilder<TModel>(htmlHelper, className);
        }

        public static MvcHtmlString DatePickerFor<TModel, TProperty>(
            this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression, string id)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(htmlHelper.EditorFor(expression, new { id = id }));
            return new MvcHtmlString(sb.ToString());
        }
    }
}